package api

import (
	"family/api/dto"
	"family/dao"
	"family/model"
	"family/pkg/constant"
	"family/pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"time"
)

var SpouseAPI = new(spouseAPI)

type spouseAPI struct{}

func (*spouseAPI) Create(c *gin.Context) {
	var addDTO dto.SpouseAddDTO
	if err := c.ShouldBindJSON(&addDTO); err != nil {
		response.ERROR(c, response.Validate, err.Error())
		return
	}
	Birthday, err := time.Parse(constant.DATE, addDTO.Birthday)
	if err != nil {
		response.ERROR(c, response.UNIVERSAL, err.Error())
	}
	DeadTime, err := time.Parse(constant.DATE, addDTO.DeadTime)
	if err != nil {
		response.ERROR(c, response.UNIVERSAL, err.Error())
		return
	}
	err = dao.SpouseDao.Create(&model.Spouse{
		People:   cast.ToUint64(addDTO.People),
		Relation: addDTO.Relation,
		Name:     addDTO.Name,
		Sex:      addDTO.Sex,
		Birthday: Birthday,
		DeadTime: DeadTime,
	})
	if err != nil {
		response.ERROR(c, response.UNIVERSAL, err.Error())
		return
	}
	response.OK(c, nil)
}

func (*spouseAPI) Details(c *gin.Context) {
	param := c.Param("ID")
	info, err := dao.SpouseDao.Info(param)
	if err != nil {
		response.ERROR(c, response.UNIVERSAL, err.Error())
		return
	}
	DTO := &dto.SpouseDTO{
		ID:       cast.ToString(info.ID),
		People:   cast.ToString(info.People),
		Relation: info.Relation,
		Name:     info.Name,
		Sex:      info.Sex,
		Birthday: info.Birthday.Format(constant.DATE),
		DeadTime: info.DeadTime.Format(constant.DATE),
	}
	response.OK(c, DTO)
}

func (*spouseAPI) Page(c *gin.Context) {
	id := cast.ToUint64(c.Query("People"))
	models, i, err := dao.SpouseDao.Page(id)
	if err != nil {
		return
	}
	var DTOS []*dto.SpouseDTO
	for _, item := range models {
		DTO := &dto.SpouseDTO{
			ID:       cast.ToString(item.ID),
			People:   cast.ToString(item.People),
			Relation: item.Relation,
			Name:     item.Name,
			Sex:      item.Sex,
			Birthday: item.Birthday.Format(constant.DATE),
			DeadTime: item.DeadTime.Format(constant.DATE),
		}
		DTOS = append(DTOS, DTO)
	}
	response.Page(c, i, DTOS)
}

func (*spouseAPI) Update(c *gin.Context) {
	var updateDTO dto.SpouseUpdateDTO
	if err := c.ShouldBindJSON(&updateDTO); err != nil {
		response.ERROR(c, response.Validate, err.Error())
		return
	}
	Birthday, err := time.Parse(constant.DATE, updateDTO.Birthday)
	if err != nil {
		response.ERROR(c, response.UNIVERSAL, err.Error())
		return
	}

	DeadTime, err := time.Parse(constant.DATE, updateDTO.DeadTime)
	if err != nil {
		response.ERROR(c, response.UNIVERSAL, err.Error())
		return
	}

	err = dao.SpouseDao.Update(&model.Spouse{
		ID:       cast.ToUint64(updateDTO.ID),
		People:   cast.ToUint64(updateDTO.People),
		Relation: updateDTO.Relation,
		Name:     updateDTO.Name,
		Sex:      updateDTO.Sex,
		Birthday: Birthday,
		DeadTime: DeadTime,
	})
	if err != nil {
		response.ERROR(c, response.UNIVERSAL, err.Error())
		return
	}
	response.OK(c, nil)
}

func (*spouseAPI) Delete(c *gin.Context) {
	var deleteDTO dto.SpouseDeleteDTO
	if err := c.ShouldBindJSON(&deleteDTO); err != nil {
		response.ERROR(c, response.Validate, err.Error())
		return
	}
	err := dao.SpouseDao.Delete(deleteDTO.Ids)
	if err != nil {
		response.ERROR(c, response.UNIVERSAL, err.Error())
		return
	}
	response.OK(c, nil)
}
